//
//  PhotoImageModel.m
//  WuliHanhan
//
//  Created by chen tang on 16/3/9.
//  Copyright © 2016年 LVpeng. All rights reserved.
//

#import "PhotoImageModel.h"
#import "Header.h"

#import <Photos/Photos.h>

@interface PhotoImageModel ()
{
    NSMutableArray *imageArray;
    NSMutableArray *dataArray;
    NSMutableArray *titleStringArray;
    NSMutableArray *sourceArray;
    
}

@end



@implementation PhotoImageModel


sloganIntence(PhotoImageModel);







+ (id)startReadPhotos
{
    
    return self;
}

- (void)getOriginalImages
{
    // 获得所有的自定义相簿
//    PHFetchResult<PHAssetCollection *> *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
//    // 遍历所有的自定义相簿
//    for (PHAssetCollection *assetCollection in assetCollections) {
////        [self enumerateAssetsInAssetCollection:assetCollection original:YES];
//    }
    
    // 获得相机胶卷
//    PHAssetCollection *cameraRoll = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil].lastObject;
    // 遍历相机胶卷,获取大图
//    [self enumerateAssetsInAssetCollection:cameraRoll original:YES];
}


- (void)getThumbnailImages
{
    // 获得所有的自定义相簿
//        PHFetchResult<PHAssetCollection *> *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
//        // 遍历所有的自定义相簿
//        for (PHAssetCollection *assetCollection in assetCollections) {
////            [self enumerateAssetsInAssetCollection:assetCollection original:NO];
//        }
//    // 获得相机胶卷
//    PHAssetCollection *cameraRoll = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil].firstObject;
//    [self enumerateAssetsInAssetCollection:cameraRoll original:NO];
}
/**
 *  遍历相簿中的所有图片
 *  @param assetCollection 相簿
 *  @param original        是否要原图
 */
- (NSArray *)enumerateAssetsInAssetCollection:(PHAssetCollection *)assetCollection original:(BOOL)original
{
    NSLog(@"相簿名:%@", assetCollection.localizedTitle);
    
    imageArray = [NSMutableArray new];
    sourceArray = [NSMutableArray new];
    
//    titleStringArray = [NSMutableArray new];
//    [titleStringArray addObject:assetCollection.localizedTitle];
    
    
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
    // 同步获得图片, 只会返回1张图片
    options.synchronous = YES;
    
    
        PHFetchOptions *optionfrc = [[PHFetchOptions alloc] init];
    optionfrc.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
    
    // 获得某个相簿中的所有PHAsset对象
    PHFetchResult *assets = [PHAsset fetchAssetsInAssetCollection:assetCollection options:optionfrc];
    for (PHAsset *asset in assets) {
        
        if (asset.mediaType == PHAssetMediaTypeImage)
        {
            
            CGSize size = original ? CGSizeMake(asset.pixelWidth, asset.pixelHeight) : CGSizeZero;
            [sourceArray addObject:asset];
            
            // 从asset中获得图片
            [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
//                NSLog(@"%@", result);
                
                [imageArray addObject:result];
            }];
            
//                        [[PHImageManager defaultManager] requestImageDataForAsset:asset options:options resultHandler:^(NSData *__nullable imageData, NSString *__nullable dataUTI, UIImageOrientation orientation, NSDictionary *__nullable info){
//            
//                            UIImage *image = [UIImage imageWithData:imageData];
//            //                NSLog(@"%@", image);
//            
//                            [dataArray addObject:image];
//                        }];
        }
        
        // 是否要原图
    }
    
    
    //传给缩略图
//    __callBackImageWithTumb(imageArray);
    
    
    
//    if ([_MPhotoImageModelDelegate respondsToSelector:@selector(callBackWithImageArrayTumb:)]) {
//        
//        [_MPhotoImageModelDelegate callBackWithImageArrayTumb:imageArray];
//    }
    
    return imageArray;
}


- (NSArray *)titleArray
{
    NSMutableArray *mutableArray = [NSMutableArray new];
    
    
    PHFetchResult<PHAssetCollection *> *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
    // 遍历所有的自定义相簿
    for (PHAssetCollection *assetCollection in assetCollections) {
        [mutableArray addObject:assetCollection.localizedTitle];
        
        //        [self enumerateAssetsInAssetCollection:assetCollection original:NO];
    }
    
    [mutableArray insertObject:@"相机胶卷" atIndex:0];
    
    return mutableArray;
}


- (void)gettitWithIMage
{
    
}


////选中获取照片
//- (void)setSelectAtIndenx:(int)index
//{
//    if (index == 0)
//    {
//        // 获得相机胶卷
//        PHAssetCollection *cameraRoll = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil].firstObject;
//        [self enumerateAssetsInAssetCollection:cameraRoll original:NO];
//
//    }
//    else
//    {
//        
//    }
//}



//选中获取照片
- (void)setSelectAtIndenx:(int)index WithCallBack:(callBackImageWithTumb)tumb
{
    if (index == 0)     //获得相册照片
    {
        PHAssetCollection *cameraRoll = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil].firstObject;
        [self enumerateAssetsInAssetCollection:cameraRoll original:NO];
//        tumb([self enumerateAssetsInAssetCollection:cameraRoll original:NO]);

    }
    else
    {
        
    }
    tumb(imageArray, sourceArray);
    
}



//获得原始图片
- (void)selectAsset:(PHAsset *)assest getOrnImage:(callBackImage)imagetumb
{
    
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
    // 同步获得图片, 只会返回1张图片
    options.synchronous = YES;
//
//    
//    //    PHFetchOptions *optionfrc = [[PHFetchOptions alloc] init];
//    // 获得某个相簿中的所有PHAsset对象
//    PHFetchResult *assets = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];

    
//    [[PHImageManager defaultManager] requestImageForAsset:assest targetSize:CGSizeMake(assest.pixelWidth, assest.pixelHeight) contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
//        //                NSLog(@"%@", result);
//        
////        NSLog(@"%@", result);
//        imagetumb(result);
//    }];
    
    
    [[PHImageManager defaultManager] requestImageDataForAsset:assest options:options resultHandler:^(NSData *__nullable imageData, NSString *__nullable dataUTI, UIImageOrientation orientation, NSDictionary *__nullable info){
        
        imagetumb(imageData);
    }];
    
}



@end
